package ua.org.groovy.gs.botworld;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;


public class Baloon {

	private int posX;
	
	private int posY;
	
	private BufferedImage image;
	
	public Baloon(int posX, int posY) {
		this.posX = posX;
		this.posY = posY;
	}
	
	public void setPosition(int x, int y) {
		this.posX = x;
		this.posY = y;
	}
	
	private BufferedImage getCacheImage() {
		GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().
		getDefaultScreenDevice().getDefaultConfiguration();
		return gc.createCompatibleImage(50, 32, Transparency.TRANSLUCENT);
	}
	
	public void draw(Graphics g) {
		if (image == null) {
			image = getCacheImage();
			Graphics2D g2d = image.createGraphics();
			AlphaComposite composite = 
				AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
			g2d.setComposite(composite);
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
					RenderingHints.VALUE_ANTIALIAS_ON);
			g2d.setColor(new Color(0xCCCCCC));
			g2d.fillRoundRect(0, 0, 50, 20, 10, 10);
			Polygon poly = new Polygon();
			poly.addPoint(15, 20);
			poly.addPoint(22, 20);
			poly.addPoint(7, 35);
			g2d.fill(poly);
			g2d.setColor(Color.RED.darker().darker());
			g2d.setFont(g2d.getFont().deriveFont(Font.BOLD));
			g2d.drawString("ouch!!!", 5, 14);
			
			g2d.dispose();
		}
		g.drawImage(image, posX, posY, null);
	}
}